package com.CampusLife.controller;


import  com.CampusLife.bean.User;
import com.CampusLife.until.ImagesUpload;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import  com.CampusLife.service.IUerService;
import  com.CampusLife.until.Encrypt;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author 赵海军
 * @createTime 2020-01-16 17:49
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Resource
    private IUerService uerService;

    /**
     *
       查询所有用户
     */

    @RequestMapping("/query")
    public String query(Model model){
      List<User> list=uerService.queryAll();
      model.addAttribute("list",list);
      return "admin/userbasic";
    }
    /**
     * 进入用户添加页面
     */
    @RequestMapping("into-add")
    public String intoadd(){
        return "admin/adduser";
    }
    /**
     * 管理员添加用户
     */
      @RequestMapping("addUser")
      public String addUser(String username){
          String password="75eecd25c2b9c5b97cadb8d623b1f3ce";
          User user=new User();
          user.setUsername(username);
          user.setPassword(password);
          uerService.addUserName(user);
          return "redirect:/user/query";
    }
    /**
     * 重置密码
     */
    @RequestMapping("/updatePasswordAdmin")
    public String updatePasswordAdmin(int id){
        User user=new  User();
        user.setId(id);
        user.setPassword("75eecd25c2b9c5b97cadb8d623b1f3ce");
        uerService.updatePasswordAdmin(user);
        return "redirect:/user/query";
    }
    /**
     *用户上传个人信息
     */
    @RequestMapping(path = "/addNew", method = RequestMethod.POST)
        public String addUser(@RequestParam("file")MultipartFile file, HttpServletRequest request,
                         HttpServletResponse response, ModelMap model,String username,String password,String phone,String address,String nickname) throws Exception {
        ImagesUpload imagesUpload =new ImagesUpload();
        String photo=  imagesUpload.Upload(file);
        String ps=Encrypt.MD5(password)+"wun";
                String  ps1=Encrypt.MD5(ps);
                User user=new User();
                user.setUsername(username);
                user.setPassword(ps1);
                user.setAddress(address);
                user.setPhone(phone);
                user.setNickname(nickname);
                user.setPhoto(photo);
                uerService.add(user);
          return "jsp/cc";
        }
        //跳转到修改页面
        @RequestMapping("/jumpUpdateUserEdit")
    public String jumpUpdateUserEdit(Integer id,Model model){

        //数据回显
        User user = uerService.queryById(id);
        model.addAttribute("user",user);
        return "jsp/update";
    }
    //修改·用户·    /图片上传
    @RequestMapping(path = "/updateUser", method = RequestMethod.POST)
    public String updateUser(@RequestParam("file")MultipartFile file, HttpServletRequest request,
                         HttpServletResponse response, ModelMap model,Integer id, String username,String password,String phone,String address,String nickname) throws Exception {

            ImagesUpload imagesUpload =new ImagesUpload();
            String photo=  imagesUpload.Upload(file);

            User user=new User();
            user.setId(id);
            user.setUsername(username);
            user.setPassword(password);
            user.setAddress(address);
            user.setPhone(phone);
            user.setNickname(nickname);
            user.setPhoto(photo);
            uerService.update(user);

        return "redirect:/user/query";
    }
    /**
     * 根据Id删除用户
     */

  @RequestMapping("/deleteUser")
    public String deleteUser( Integer id){

        uerService.delete(id);
          return "redirect:/user/query";
  }
    /**
     * 根据学号模糊查询
     */
    @RequestMapping("/ByUsername")
    public String ByUsername( String username,Model model){
        List<User> list=uerService.ByUsername(username);
        model.addAttribute("list",list);
        return "admin/userbasic";

}

    /**
     * 根据用户昵称模糊查询
     * @param nickname
     * @param model
     * @return
     */
    @RequestMapping("/ByNikeName")
    public String ByNikeName( String nickname,Model model){
        List<User> list=uerService.ByNikeName(nickname);
        model.addAttribute("list",list);
        return "admin/userbasic";

    }
}





